package com.dehui.traders.ui.main;

import android.content.Context;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.view.View;

import com.dehui.traders.bean.BaseResponse;
import com.dehui.traders.enums.MainTab;
import com.dehui.traders.service.CallListener;
import com.dehui.traders.service.NetImpl;
import com.dehui.traders.ui.optioanl.AddOptionalStockActivity;
import com.dehui.traders.ui.main.MainActivity;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by hqq on 18.3.29.
 */

public class MainViewModel extends BaseViewModel{

    private MainActivity activity;

    public ObservableField<String> title = new ObservableField<>("首页");

    public ObservableInt searchVisity = new ObservableInt(View.VISIBLE);

    public BindingCommand tabChangeCommand = new BindingCommand(s->{
        title.set(String.valueOf(s));
        if ("首页".equals(title.get())){
            searchVisity.set(View.VISIBLE);
        }else{
            searchVisity.set(View.GONE);
        }
    });

    public BindingCommand searchCommand = new BindingCommand(()->{
        AddOptionalStockActivity.start(activity);
    });

    public ObservableField<MainTab[]> mainTabs = new ObservableField<>(MainTab.values());

    public MainViewModel(MainActivity activity, Context context) {
        super(context);
        this.activity = activity;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        getTimes();
    }

    private void getTimes() {
        Call<BaseResponse<Integer>> call = NetImpl.getTime();
        call.enqueue(new CallListener<BaseResponse<Integer>>() {
            @Override
            public void _onResponse(Response<BaseResponse<Integer>> response) {
                BaseResponse<Integer> intBaseResponse = response.body();
                int status = intBaseResponse.status;
                if(status == 200){
                    MainActivity.isRepeat = (intBaseResponse.result == 0);
                }
            }

            @Override
            public void _onFailure() {

            }
        });
    }

}
